var entity = require('../entity');
var FactionMember = entity.FactionMember;


exports.newAndSave = function(faction, user, card, callback){
    //console.log('--------'+JSON.stringify(user));
    //console.log('--------'+JSON.stringify(card));
    var fm = new FactionMember();
    fm.faction = faction._id;
    fm.member = {
        id: user.id || user._id.toHexString(),
        name: user.name,
        gender: user.gender,
        company: card.company,
        position: card.position,
        business: card.business,
        product: card.product,
        from: faction.from || null,
        score: 0
    }
    fm.save(function(err, fm){
        callback(err, fm)
    })
}

exports.pageMembersByFaction = function(factionId, pageNo, pageSize, callback){
    var start = (pageNo - 1)*pageSize;
    FactionMember.find({faction: factionId})
        .skip(start)
        .limit(pageSize)
        .exec(function(err, members){
        callback(err, members);
    })
}

exports.getAllMembers = function(factionId, callback){
    FactionMember.find({faction: factionId}).exec(function(err, factions){
        callback(err, factions);
    })
}

exports.addScore = function(factionId, userId, score, callback){
    FactionMember.findOne({faction: factionId, 'member.id':userId})
        .exec(function(err, fm){
            if(fm){
                fm.score = fm.score + score;
                if(fm.score >= 0){
                    FactionMember.update({faction: factionId, 'member.id':userId},
                        {$set :{score: fm.score}}, {}, function(err, resp){
                            callback(err, fm.score);
                        })
                }else{
                    callback(err, fm.score);
                }
            }else{
                callback(err, fm)
            }
    })
}

exports.getFactionMembersByUserId = function(userId, callback){
    FactionMember.find({'member.id':userId})
        .populate('faction')
        .exec(function(err, fms){
        callback(err, fms);
    })
}

exports.getFmByUserAndFactionId = function(userId, factionId, callback){
    FactionMember.findOne({'member.id':userId, faction: factionId})
        .populate('faction')
        .exec(function(err, fm){
            callback(err, fm);
        })
}

exports.setPrivacy4User = function(userId, factionId, flag, colume, callback){
    FactionMember.findOne({faction: factionId, 'member.id':userId})
        .exec(function(err, fm){
            var permission = fm.permission;
            if(colume === 'newAndALl'){
                permission = {
                    showCard: false,
                    showMobile: false,
                    showCompany: true,
                    showBusiness: true,
                    showPosition: true,
                    showProduct: true,
                    showEmail: true
                }
                //permission.showCard = flag;
            }
            if(colume === 'mobile'){
                permission.showMobile = flag;
            }
            if(colume === 'company'){
                permission.showCompany = flag;
            }
            if(colume === 'position'){
                permission.showPosition = flag;
            }
            if(colume === 'business'){
                permission.showBusiness = flag;
            }
            if(colume === 'product'){
                permission.showProduct = flag;
            }
            if(colume === 'email'){
                permission.showEmail = flag;
            }
            FactionMember.update({_id: fm._id}, {$set:{permission:permission}}, {}, function(err, data){
                callback(err, data);
            })
    })
}

exports.getFactionMembersByUserIdAndFactionId = function(userId, factionId, callback){
    FactionMember.findOne({faction: factionId, 'member.id':userId})
        .populate('faction')
        .exec(function(err, fm){
            callback(err, fm);
        })
}

exports.getFactionPermission = function(userId, factionId, callback){
    if(factionId){
        FactionMember.findOne({faction: factionId, 'member.id':userId})
            .exec(function(err, fm){
                callback(err, fm.permission);
            })
    }else{
        callback(null, null);
    }
}

exports.removeByUserIdFactionId = function(userId, factionId, callback){
    FactionMember.remove({faction: factionId, 'member.id':userId}, function(err, data){
        callback(err, data);
    })
}

exports.updateFactionsMembersInfo = function(userid, card, callback){
    FactionMember.find({'member.id': userid}).exec(function(err, fms){
        fms.forEach(function(fm){
            var newMember = fm.member;
            newMember.name = card.name;
            newMember.company = card.company;
            newMember.position = card.position;
            newMember.business = card.business;
            newMember.product = card.product;
            FactionMember.update({_id: fm._id}, {$set: {member: newMember}}, {}, function(err, resp){
                if(err) console.log(err);
            })
        });
        callback(null, null);
    })
}